<?php
/**
 * Groups module for XOOPS
 *
 * You may not change or alter any portion of this comment or credits
 * of supporting developers from this source code or any supporting source code 
 * which is considered copyrighted (c) material of the original comment or credit authors.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 * @copyright       The Groups Project http://code.google.com/p/groupsforxoops/
 * @license         http://www.fsf.org/copyleft/gpl.html GNU public license
 * @package         groups
 * @since           2.3.0
 * @author          Hui Xiao < xh.8326@gmail.com >
 * @version         $Id: action.submit.php Sat Jul 18 12:46:12 CST 2009
 */

include "conf.inc";
$myts = MyTextSanitizer::getInstance();

$ac = isset($_POST["ac"]) ? $_POST["ac"] : "";

if ( !is_object($xoopsUser) ) {
	redirect_header(XOOPS_URL."/user.php",5,_NOPERM);
}
$groups_handler = xoops_getmodulehandler("groups");
$events_handler = xoops_getmodulehandler("events","groups");
switch ($ac) {
    case "savetopic":
        $group_id = isset($_POST["group_id"]) ? intval($_POST["group_id"]) : 0 ;
        $first = isset($_POST["first"]) ? intval($_POST["first"]) : 0 ;
        $column_id = isset($_POST["column_id"]) ? intval($_POST["column_id"]) : 0 ;
        $threads_subject = $post_subject = isset($_POST["post_subject"]) ? trim($_POST["post_subject"]) : "";
        $threads_icon = isset($_POST["threads_icon"]) ? trim($_POST["threads_icon"]) : 0;
        $post_content = isset($_POST["post_content"]) ? trim($_POST["post_content"]) : "";
        
        if ( empty($threads_subject) || empty($post_content) ) {
            redirect_header("groups.php?id={$group_id}", 3, _GP_ERROR_FIELD_WRITE);
        }
        
        if (!$GLOBALS["xoopsSecurity"]->check()) {
            redirect_header("post.php?group_id={$group_id}", 3, implode("<br />", $GLOBALS["xoopsSecurity"]->getErrors()));
            exit();
        }
        
        // new topic
        if ( !empty($first) ) {
            // add to threads table
            $threads_handler = xoops_getmodulehandler("threads");
            $tid = $threads_handler->setNewTopic($group_id, $threads_subject, $xoopsUser->getVar("uid"), $xoopsUser->getVar("uname"), $xoopsUser->getVar("uid"), $xoopsUser->getVar("uname"),$column_id, $threads_icon ) ;
            if ( !empty($tid) ) {
			    // create post
                $posts_handler = xoops_getmodulehandler("posts");
                $sign = $xoopsUser->getVar("user_sig");
                $post_id = $posts_handler->setNewPost($group_id, $tid, $first, $post_subject, $post_content,  $xoopsUser->getVar("uid"), $xoopsUser->getVar("uname"), $column_id, $sign);
                // add groups topic num and post num
			    $groups_handler->changeTopics($group_id,1,0);
			    
                if ( $post_id ) {
                    if ( $_FILES ) {
                        $n = 0;
                    	for ($i=0;$i<$xoopsModuleConfig["attnum"]; $i++ ) {
                			if ( !$_FILES[$_POST["xoops_upload_file"][$i]]["size"] ) continue;
                			$n++;
                    	}
                    	
                    	if ( $n > 0 ) {
                    	    $attachments_handler = xoops_getmodulehandler("attachments");
                    		$att_num = $attachments_handler->setAttachments($tid,$post_id,$xoopsUser->getVar("uid"));
                    		if ( !empty($att_num) ) {
                    		    $threads_handler->changeAttachments($tid,$att_num);
                    		    $posts_handler->changeAttachments($post_id,$att_num);
                    		}
            			}
                    }
                }
                /** 产生一个事件 **/
                $_type = !empty($att_num) ? 105 : 104; 
		        $events_handler->eventApi($xoopsUser->getVar("uid"),$tid,$_type,$threads_subject,$group_id,$post_content);
                redirect_header("viewthread.php?tid={$tid}",5,_GP_SUCCEED);
            }
        } else {
            $tid = isset($_POST["tid"]) ? intval($_POST["tid"]) : 0;
            if ( !empty($tid) ) {
                $dateline = time();
                $posts_handler = xoops_getmodulehandler("posts");
                $sign = $xoopsUser->getVar("user_sig");
                $post_id = $posts_handler->setNewPost($group_id, $tid, 0, $post_subject, $post_content,  $xoopsUser->getVar("uid"), $xoopsUser->getVar("uname"), 0, $sign, $dateline);
                
                if ( !empty($post_id) ) {
                    // change group topics num
                    $groups_handler->changeTopics($group_id,0,1);
                    
                    $threads_handler = xoops_getmodulehandler("threads");
                    $threads_handler->changeReplies($tid);
                    
                    $threads_handler->setLastUploadTopic($tid,$xoopsUser->getVar("uid"), $xoopsUser->getVar("uname"),$dateline);
                    
                    if ( $_FILES ) {
                        $n = 0;
                    	for ($i=0;$i<$xoopsModuleConfig["attnum"]; $i++ ) {
                			if ( !$_FILES[$_POST["xoops_upload_file"][$i]]["size"] ) continue;
                			$n++;
                    	}
                    	if ( $n > 0 ) {
                    	    $attachments_handler = xoops_getmodulehandler("attachments");
                    		$att_num = $attachments_handler->setAttachments($tid,$post_id,$xoopsUser->getVar("uid"));
                    		if ( !empty($att_num) ) {
                    		    $threads_handler->changeAttachments($tid,$att_num);
                    		    $posts_handler->changeAttachments($post_id,$att_num);
                    		}
            			}
                    }
                    
                    $StartPage = ($posts_handler->getPostInStartPage($tid, $post_id, $xoopsModuleConfig["topicsitems"]) - 1) * $xoopsModuleConfig["topicsitems"];
                    $reurl = !empty($StartPage) ? "viewthread.php?tid={$tid}&start={$StartPage}#n{$post_id}" : "viewthread.php?tid={$tid}#n{$post_id}" ;
                    
                    /** 产生一个事件 **/
                    $_type = !empty($att_num) ? 109 : 106; 
                    $_obj = $threads_handler->get($tid);
                    $_threads_subject = $_obj->getVar("threads_subject");
		            $events_handler->eventApi($xoopsUser->getVar("uid"),$tid,$_type,$_threads_subject,$group_id,$post_content);
                    
                    redirect_header($reurl,5,_GP_SUCCEED);
                }
             }
             redirect_header("viewthread.php?tid={$tid}",5,_GP_FAILED);
        }
        redirect_header("groups.php?id={$group_id}",5,_GP_FAILED);
    break;
    
    case "modtopic":
        $group_id = isset($_POST["group_id"]) ? intval($_POST["group_id"]) : 0 ;
        $post_id = isset($_POST["post_id"]) ? intval($_POST["post_id"]) : 0 ;
        $start = isset($_POST["start"]) ? intval($_POST["start"]) : 0 ;
        $tid = isset($_POST["tid"]) ? intval($_POST["tid"]) : 0 ;
        $first = isset($_POST["first"]) ? intval($_POST["first"]) : 0 ;
        $column_id = isset($_POST["column_id"]) ? intval($_POST["column_id"]) : 0 ;
        $threads_subject = $post_subject = isset($_POST["post_subject"]) ? trim($_POST["post_subject"]) : "";
        $threads_icon = isset($_POST["threads_icon"]) ? trim($_POST["threads_icon"]) : 0;
        $post_content = isset($_POST["post_content"]) ? trim($_POST["post_content"]) : "";
        
        if ( empty($post_subject) || empty($post_content) ) {
            redirect_header("groups.php?id={$group_id}", 3, _GP_ERROR_FIELD_WRITE);
        }
        
        if (!$GLOBALS["xoopsSecurity"]->check()) {
            redirect_header("post.php?post_id={$post_id}", 3, implode("<br />", $GLOBALS["xoopsSecurity"]->getErrors()));
            exit();
        }
        
        $threads_handler = xoops_getmodulehandler("threads");
        if ( !empty($first) ) {
            //modified threads table
            $tid = $threads_handler->setUploadTopic($tid, $threads_subject, $column_id, $threads_icon) ;
        }
        
        $posts_handler = xoops_getmodulehandler("posts");
        $post_id = $posts_handler->setUploadPost($post_id, $post_subject, $post_content,$column_id) ;
        
        if ( $post_id ) {
            if ( $_FILES ) {
                $n = 0;
            	for ($i=0;$i<$xoopsModuleConfig["attnum"]; $i++ ) {
        			if ( !$_FILES[$_POST["xoops_upload_file"][$i]]["size"] ) continue;
        			$n++;
            	}
            	if ( $n > 0 ) {
            	    $attachments_handler = xoops_getmodulehandler("attachments");
            		$att_num = $attachments_handler->setAttachments($tid,$post_id,$xoopsUser->getVar("uid"));
            		if ( !empty($att_num) ) {
            		    $threads_handler->changeAttachments($tid,$att_num);
            		    $posts_handler->changeAttachments($post_id,$att_num);
            		}
    			}
            }
            $reurl = !empty($start) ? "viewthread.php?tid={$tid}&start={$start}#n{$post_id}" : "viewthread.php?tid={$tid}#n{$post_id}" ;
            /** 产生一个事件 **/
            $events_handler->eventApi($xoopsUser->getVar("uid"),$tid,107,$threads_subject,$group_id,$post_content);
		            
            redirect_header($reurl,5,_GP_SUCCEED);
        }
        
        redirect_header("viewthread.php?tid={$tid}",5,_GP_FAILED);
    break;
    
    case "replytopic":
        $group_id = isset($_POST["group_id"]) ? intval($_POST["group_id"]) : 0 ;
        $tid = isset($_POST["tid"]) ? intval($_POST["tid"]) : 0 ;
        $start = isset($_POST["start"]) ? intval($_POST["start"]) : 0 ;
        $post_subject = isset($_POST["post_subject"]) ? trim($_POST["post_subject"]) : "";
        $post_content = isset($_POST["post_content"]) ? trim($_POST["post_content"]) : "";
        
        $reurl = !empty($tid) ? "viewthread.php?tid={$tid}" :  "groups.php?group_id={$group_id}" ;
        
        if ( empty($post_subject) || empty($post_content) ) {
            redirect_header($reurl, 3, _GP_ERROR_FIELD_WRITE);
        }
        if (!$GLOBALS["xoopsSecurity"]->check()) {
            redirect_header($reurl, 3, implode("<br />", $GLOBALS["xoopsSecurity"]->getErrors()));
            exit();
        }
        unset($reurl);
        $dateline = time();
        $posts_handler = xoops_getmodulehandler("posts");
        $sign = $xoopsUser->getVar("user_sig");
        $post_id = $posts_handler->setNewPost($group_id, $tid, 0, $post_subject, $post_content,  $xoopsUser->getVar("uid"), $xoopsUser->getVar("uname"), 0, $sign, $dateline);
        
        if ( !empty($post_id) ) {
            // change group topics num
            $groups_handler->changeTopics($group_id,0,1);
            
            $threads_handler = xoops_getmodulehandler("threads");
            $threads_handler->changeReplies($tid);
            
            $threads_handler->setLastUploadTopic($tid,$xoopsUser->getVar("uid"), $xoopsUser->getVar("uname"),$dateline);
            $reurl = !empty($start) ? "viewthread.php?tid={$tid}&start={$start}#n{$post_id}" : "viewthread.php?tid={$tid}#n{$post_id}" ;
            
            /** 产生一个事件 **/
            $_obj = $threads_handler->get($tid);
            $_threads_subject = $_obj->getVar("threads_subject");
            $events_handler->eventApi($xoopsUser->getVar("uid"),$tid,106,$_threads_subject,$group_id,$post_content);
            redirect_header($reurl,5,_GP_SUCCEED);
        }
        redirect_header("viewthread.php?tid={$tid}",5,_GP_FAILED);
    break;
    
    default:
        redirect_header("index.php");
    break;
}
?>